package api

import (
	"anban/models"
	"anban/service"
	"anban/utils"
	"fmt"
	"net/url"
)

type StudentController struct {
	BaseController
}

// 家长守护成员
func (c *StudentController) ChildrenMember() {
	var res *utils.ResJsonStruct

	userId := c.GetSession("miniUserId")
	p := map[string]interface{}{}
	p["id"] = userId
	user := service.GetUserInfo(p)

	p = map[string]interface{}{}
	p["relation"] = true
	p["api"] = true
	p["status"] = 1
	p["familySn"] = user.FamilySn
	_, memberList := service.GetStudentList(p)

	res = utils.ResJson(0, "成功获取数据", memberList)
	c.Data["json"] = res
	c.ServeJSON()
	return
}

// 添加守护成员
func (c *StudentController) AddChildrenMember() {
	var res *utils.ResJsonStruct
	input := c.Input()

	if len(input["realName"][0]) <= 0 {
		res = utils.ResJson(1100, "请输入姓名", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	if len(input["idCard"][0]) <= 0 {
		res = utils.ResJson(1100, "请输入身份证号", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}

	p := map[string]interface{}{}
	p["realName"] = input["realName"][0]
	p["idCard"] = input["idCard"][0]
	student := service.GetStudentInfo(p)
	if student.Id <= 0 {
		res = utils.ResJson(1100, "姓名或身份证号不正确", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	if len(student.FamilySn) > 0 {
		res = utils.ResJson(1100, fmt.Sprintf("添加失败，%s已经存在了", input["realName"][0]), "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}

	var addInput url.Values
	var editInput url.Values

	userId := c.GetSession("miniUserId")
	p = map[string]interface{}{}
	p["id"] = userId
	user := service.GetUserInfo(p)

	p = map[string]interface{}{}
	p["familySn"] = user.FamilySn
	_, parentMemberList := service.GetUserList(p)
	for _, parentMember := range parentMemberList {
		addInput = url.Values{
			"user_id": {fmt.Sprintf("%d", parentMember.Id)},
			"student_id": {fmt.Sprintf("%d", student.Id)},
		}
		_, err := service.AddUserStudent(addInput)
		if err != nil {
			res = utils.ResJson(1100, "操作失败，请稍候重试", "")
			c.Data["json"] = res
			c.ServeJSON()
			return
		}
	}

	editInput = url.Values{
		"id": {fmt.Sprintf("%d", student.Id)},
		"family_sn": {user.FamilySn},
	}
	_, err := service.EditStudent(editInput)
	if err != nil {
		res = utils.ResJson(1100, "操作失败，请稍候重试", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}

	res = utils.ResJson(0, "操作成功", "")
	c.Data["json"] = res
	c.ServeJSON()
}

// 学生家长列表
func (c *StudentController) DetailByTeacher() {
	var res *utils.ResJsonStruct
	var p map[string]interface{}
	studentId := c.GetString("id")

	if len(studentId) <= 0 {
		res = utils.ResJson(1300, "学生Id异常", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}

	p = map[string]interface{}{}
	p["id"] = studentId
	p["api"] = true
	p["relation"] = true
	studentInfo := service.GetStudentInfo(p)

	var familyList []*models.User
	if len(studentInfo.FamilySn) > 0 {
		p = map[string]interface{}{}
		p["api"] = true
		p["familySn"] = studentInfo.FamilySn
		p["status"] = 1
		p["relation"] = true
		_, familyList = service.GetUserList(p)
	}

	data := struct {
		StudentInfo      *models.Student `json:"StudentInfo"`
		FamilyMemberList []*models.User  `json:"FamilyMemberList"`
	}{
		StudentInfo:      studentInfo,
		FamilyMemberList: familyList,
	}

	res = utils.ResJson(0, "成功获取数据", data)
	c.Data["json"] = res
	c.ServeJSON()
	return
}